package com.codoon.unova.ui;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import com.codoon.unova.util.ConfigManager;
import com.uwatch.helowin.R;

public class SettingTargetActivity extends BaseActivity {
	private EditText et_target;
	private InputMethodManager mInputManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_target);
		mInputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		et_target = (EditText) findViewById(R.id.et_target);
		int target = ConfigManager.getTarget(getApplicationContext());
		et_target.setText(target == 0 ? "10000" : target + "");
		try {
			int textlength = et_target.getText().toString().length();
			et_target.setSelection(textlength);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void showPrevious() {
		// TODO Auto-generated method stub
		super.showPrevious();
		String target = et_target.getText().toString().trim();
		if (TextUtils.isEmpty(target)) {
			ConfigManager.saveTarget(getApplicationContext(), 0);
		} else {
			ConfigManager.saveTarget(getApplicationContext(), Integer.parseInt(target));
		}
		ConfigManager.setStringValue(getApplicationContext(), ConfigManager.SETTING_VIEW, "settingtarget");		
		
		if (ConfigManager.getIntValue(getApplicationContext(), ConfigManager.CURRENT_INDEX) == 0) {
			changeView(SportsMainActivity.class, null, true);
		} else {
			changeView(SportsOneActivity.class, null, true);
		}
		overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		
		String target = et_target.getText().toString().trim();
		if (TextUtils.isEmpty(target)) {
			ConfigManager.saveTarget(getApplicationContext(), 0);
		} else {
			ConfigManager.saveTarget(getApplicationContext(), Integer.parseInt(target));
		}
		mInputManager.hideSoftInputFromWindow(et_target.getWindowToken(), 0);
	}
}
